<%#
    File name : mtk_wifi_map_bh_link_metrics.htm
    This file is used in WebUI based on LuCI
    to display the feature of EasyMesh Configurations
    named as Back-haul Link Metrics at Controller.
%>
<%+header%>

<script src="/luci-static/resources/monCon.js"></script>

<h2><a>EasyMesh Back-haul Link Metrics at Controller</a></h2>
<div class="alert-message" id="get_bh_link_metrics_msg_div">
    <img src="<%=resource%>/icons/loading.gif" alt="" style="vertical-align:middle" />
    <big><strong>Retrieving Back-haul Link Metrics at Controller!</strong></big>
</div>
<div class="alert-message error" style="display:none">
    <span><strong id="bh_link_metrics_err_msg_div"></strong></span>
</div>
<div id="bh_link_metrics_display"></div>

<script type="text/javascript">

    var max_cellspan = 2;

    function set_max_cellspan(o)
    {
        for(var k in o){
            var v = o[k];
            if (v instanceof Array){
                max_cellspan++;
                for(var a_idx=0; a_idx < v.length; a_idx++){
                    if(((typeof v[a_idx]) == "object") && v[a_idx] != null){
                        set_max_cellspan(v[a_idx]);
                    }
                    else{
                        console.log("set_max_cellspan: Incorrect Topology: Array element is not an object!",v[a_idx]);
                    }
                }
            }
            else if(((typeof v) == "object") && v != null){
                set_max_cellspan(v);
            }
        }
    }

    function set_rowspan(o)
    {
        var rowspan = 0;
        for(var k in o){
            var v = o[k];
            if (v instanceof Array){
                for(var a_idx=0; a_idx < v.length; a_idx++){
                    if(((typeof v[a_idx]) == "object") && v[a_idx] != null){
                        rowspan = rowspan + set_rowspan(v[a_idx]);
                    }
                    else{
                        console.log("set_rowspan: Incorrect Topology: Array element is not an object!",v[a_idx]);
                    }
                }
            }
            else if(((typeof v) == "object") && v != null){
                rowspan = rowspan + set_rowspan(v);
            }
            else{
                rowspan++;
            }
        }
        return rowspan;
    }

    function obj_loop(table_id, row, o)
    {
        var cell;
        for(var k in o){
            var v = o[k];
            if (v instanceof Array){
                arr_loop(table_id, k, v);
            }
            else if(((typeof v) == "object") && v != null){
                obj_loop(table_id, row, v);
            }
            else{
                if(row == null){
                    row = table_id.insertRow(-1);
                }
                cell = row.insertCell(-1);
                cell.innerHTML = k;
                cell = row.insertCell(-1);
                cell.colSpan = max_cellspan - row.cells.length + 1;
                cell.innerHTML = v;
                row = null;
            }
        }
    }

    function arr_loop(table_id, k, a)
    {
        var row, cell;
        for(var a_idx=0; a_idx < a.length; a_idx++){
            var v = a[a_idx];
            if(((typeof v) == "object") && v != null){
                row = table_id.insertRow(-1);
                cell = row.insertCell(-1);
                cell.rowSpan = set_rowspan(v);
                cell.style.verticalAlign  = "middle";
                cell.innerHTML = "<strong>" + k + ' - ' + (a_idx + 1) + "</strong>";
                obj_loop(table_id, row, v);
            }
            else{
                console.log("Incorrect Topology: Array element is not an object!",a);
            }
        }
    }

    function draw_bh_link_metrics_table(jsTopoInfo)
    {
        var topoInfoArr = jsTopoInfo['topology information'];
        if(!(topoInfoArr instanceof Array)){
            console.log("Incorrect TopologyInfo: Value of topology information is not an Array!");
            document.getElementById("get_bh_link_metrics_msg_div").style.display = "none";
            document.getElementById("bh_link_metrics_err_msg_div").innerHTML = "Incorrect TopologyInfo: Value of topology information is not an Array!<br /> UI will retry in a short while.";
            document.getElementById("bh_link_metrics_err_msg_div").parentNode.parentNode.style.display = "";
            return;
        }
        var idx_1905;
        for(idx_1905=0; idx_1905 < topoInfoArr.length; idx_1905++){
            var obj_1905 = topoInfoArr[idx_1905];
            if(!obj_1905.hasOwnProperty('Device role') ||
                parseInt(obj_1905['Device role']) != 1){
                continue;
            }
            if(!obj_1905.hasOwnProperty('backhaul link metrics')){
                console.log("No backhaul link metrics in Controller's Topology Information! Topology:",obj_1905);
                document.getElementById("get_bh_link_metrics_msg_div").style.display = "none";
                document.getElementById("bh_link_metrics_err_msg_div").innerHTML = "No backhaul link metrics in Controller's Topology Information!<br /> UI will retry in a short while.";
                document.getElementById("bh_link_metrics_err_msg_div").parentNode.parentNode.style.display = "";
                return;
            }

            var bhMetricsDb = {};
            bhMetricsDb['backhaul link metrics'] = obj_1905['backhaul link metrics'];

            var bh_link_metrics_tbl = document.createElement("TABLE");
            bh_link_metrics_tbl.setAttribute("class", "cbi-section-table");
            var dev1905fset = document.createElement("FIELDSET");
            dev1905fset.setAttribute("class", "cbi-section");
            var dev1905legend = document.createElement("LEGEND");
            var dev1905a = document.createElement("A");
            var dev1905text = document.createTextNode("Back-haul Link Metrics at Controller with AL-MAC " + obj_1905['AL MAC']);
            dev1905a.setAttribute("href", "#");
            dev1905a.appendChild(dev1905text);
            dev1905legend.appendChild(dev1905a);
            dev1905fset.appendChild(dev1905legend);
            document.getElementById("bh_link_metrics_display").appendChild(dev1905fset);

            set_max_cellspan(bhMetricsDb);
            obj_loop(bh_link_metrics_tbl, null, bhMetricsDb);

            document.getElementById("bh_link_metrics_display").appendChild(bh_link_metrics_tbl);
            document.getElementById("bh_link_metrics_display").style.display = "";
            document.getElementById("get_bh_link_metrics_msg_div").style.display = "none";
            break;
        }
        if(idx_1905 == topoInfoArr.length){
            console.log("No Controller found in Topology Information!",topoInfoArr);
            document.getElementById("get_bh_link_metrics_msg_div").style.display = "none";
            document.getElementById("bh_link_metrics_err_msg_div").innerHTML = "No Controller found in Topology Information!<br /> UI will retry in a short while.";
            document.getElementById("bh_link_metrics_err_msg_div").parentNode.parentNode.style.display = "";
            return;
        }
    }

    function get_run_time_topology()
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "mtk", "multi_ap", "get_runtime_topology")%>', null, 
            function(x)
            {
                try{
                    var r = JSON.parse(x.response);
                    if(r.status == "SUCCESS"){
                        var jsTopoInfo = JSON.parse(r['luaTopologyInfo']);
                        draw_bh_link_metrics_table(jsTopoInfo);
                    }
                    else{
                        console.log("Failed to get Topology Info!\nStatus: ",r.status);
                        document.getElementById("get_bh_link_metrics_msg_div").style.display = "none";
                        document.getElementById("bh_link_metrics_err_msg_div").innerHTML = "Failed to get Topology Info!<br />Status: " + r.status + "<br /> UI will retry in a short while.";
                        document.getElementById("bh_link_metrics_err_msg_div").parentNode.parentNode.style.display = "";
                    }
                }
                catch(e){
                    console.log("Incorrect response! Failed to get Topology Info!",e.name,e.message);
                    document.getElementById("get_bh_link_metrics_msg_div").style.display = "none";
                    document.getElementById("bh_link_metrics_err_msg_div").innerHTML = "Incorrect response! Failed to get Topology Info!<br />" + e.name + " " + e.message + "<br /> UI will retry in a short while.";
                    document.getElementById("bh_link_metrics_err_msg_div").parentNode.parentNode.style.display = "";
                }
                finally{
                    window.setTimeout(get_bh_link_metrics, 60000);
                }
            }
        );
    }

    function get_bh_link_metrics()
    {
        document.getElementById("bh_link_metrics_err_msg_div").innerHTML = "";
        document.getElementById("bh_link_metrics_err_msg_div").parentNode.parentNode.style.display = "none";
        document.getElementById("bh_link_metrics_display").style.display = "none";
        document.getElementById("bh_link_metrics_display").innerHTML = "";
        document.getElementById("get_bh_link_metrics_msg_div").style.display = "";
        get_run_time_topology();
    }

    window.onload = function(){
        MonCon.ping();
        window.setTimeout(get_bh_link_metrics, 1);
    }

</script>
<%+footer%>